package com.lyw.forum.controller;

import com.lyw.forum.common.enums.ResultCode;
import com.lyw.forum.pojo.request.ArticleReplyCreateRequest;
import com.lyw.forum.pojo.response.AppResult;
import com.lyw.forum.pojo.response.ArticleReplySelectResponse;
import com.lyw.forum.pojo.response.ArticleSelectResponse;
import com.lyw.forum.pojo.response.UserSelectResponse;
import com.lyw.forum.service.IArticleReplyService;
import com.lyw.forum.service.IArticleService;
import com.lyw.forum.service.IUserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author lyw
 */
@Slf4j
@RestController
@RequestMapping("/articleReply")
public class ArticleReplyController {

    @Resource
    private IArticleReplyService articleReplyService;

    @Resource
    private IUserService userService;

    @Resource
    private IArticleService articleService;

    /**
     * 创建回复帖子
     * @param articleReplyCreateRequest 回复帖子请求内容
     * @return
     */
    @RequestMapping("/create")
    public AppResult create(@RequestBody @Validated ArticleReplyCreateRequest articleReplyCreateRequest,
                            HttpServletRequest request) {
        //获取用户的id
        String id = request.getHeader("id");

        //获取用户信息
        UserSelectResponse userSelectResponse = userService.selectById(Integer.parseInt(id));

        //判断用户是否被禁言
        if (userSelectResponse.getState() == 1) {
            //用户已禁言 打印日志
            log.error(ResultCode.FAILED_USER_BANNED.toString());
            //但会结果
            return AppResult.fail(ResultCode.FAILED_USER_BANNED);
        }

        //设置postUserId
        articleReplyCreateRequest.setPostUserId(Integer.parseInt(id));

        //获取要回复的帖子对象
        ArticleSelectResponse articleSelectResponse = articleService.selectById(articleReplyCreateRequest.getArticleId());

        //是否存在或者已经删除
        if (articleSelectResponse == null || articleSelectResponse.getDeleteState() == 1 ) {
            //打印日志
            log.error(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
            //返回结果
            return AppResult.fail(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }

        //调用 service
        articleReplyService.creat(articleReplyCreateRequest);

        //返回结果
        return AppResult.success();
    }

    @RequestMapping("/getReplies")
    public AppResult<List<ArticleReplySelectResponse>> getRepliesByArticleId(@NotNull(message = "id不能为空") Integer id) {
        //校验帖子是否存在
        ArticleSelectResponse articleSelectResponse = articleService.selectById(id);

        if (articleSelectResponse == null || articleSelectResponse.getDeleteState() == 1) {
            //返回错误
            return AppResult.fail(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }

        //调用service
        List<ArticleReplySelectResponse> articleReplySelectResponses = articleReplyService.selectByArticleId(id);

        //返回结果
        return AppResult.success(articleReplySelectResponses);
    }
}
